/*
 * @Author: mikey.zhaopeng 
 * @Date: 2018-11-20 23:03:22 
 * @Last Modified by: mikey.zhaopeng
 * @Last Modified time: 2018-11-27 23:50:44
 */
import 'package:flutter/material.dart';
import '../constants.dart';
import '../modal/convasation.dart' show Conversation, ConversationPageData;

enum Device{
  MAC,WIN
}

class ConversationPage extends StatefulWidget {
  @override
  _ConversationPageState createState() => _ConversationPageState();
}

class _ConversationPageState extends State<ConversationPage> {
  final ConversationPageData listData = ConversationPageData.mock();
  @override
  Widget build(BuildContext context) {
    var mockConversations=listData.conversations;
    return ListView.builder(
      itemBuilder: (BuildContext context, int index) {
        if(listData.device != null){
          if(index ==0 ){
            return  _DeviceInfoItem();
          }
          return _ConversationItem(
            conversation: mockConversations[index-1],
          );
        }else{
          return _ConversationItem(
            conversation: mockConversations[index],
          );
        }
        
      },
      itemCount: listData.device != null?mockConversations.length+1: mockConversations.length,
    );
  }
}

//单独列表
class _ConversationItem extends StatelessWidget {
  const _ConversationItem({Key key, this.conversation})
      : assert(conversation != null),
        super(key: key);
  final Conversation conversation;
  @override
  Widget build(BuildContext context) {
    Widget avatar;
    if (conversation.isAvatarFromNet()) {
      avatar = Image.network(
        conversation.avatar,
        width: Constants.ConversationAvatarSize,
        height: Constants.ConversationAvatarSize,
      );
    } else {
      avatar = Image.asset(
        conversation.avatar,
        width: Constants.ConversationAvatarSize,
        height: Constants.ConversationAvatarSize,
      );
    }

    //头像容器
    Widget avatarContainer;
    if (conversation.unreadMsgCount > 0) {
      //未读消息角标
      Widget unReadMsgCountText = Container(
        width: Constants.UnReadMsgNotifyDotSize,
        height: Constants.UnReadMsgNotifyDotSize,
        alignment: Alignment.center,
        decoration: BoxDecoration(
            borderRadius:
                BorderRadius.circular(Constants.UnReadMsgNotifyDotSize / 2.0),
            color: Color(AppColors.NotifyDotBg)),
        child: Text(conversation.unreadMsgCount>=100?'···':conversation.unreadMsgCount.toString(), style: AppStyles.UnReadMsgCountStyle),
      );
      avatarContainer = Stack(
        overflow: Overflow.visible,
        children: <Widget>[
          avatar,
          Positioned(
            right: -6.0,
            top: -6.0,
            child: unReadMsgCountText,
          ),
        ],
      );
    } else {
      avatarContainer = avatar;
    }

    //勿扰模式图标
    Widget muteIcon = Icon(IconData(
      0xe755,
      fontFamily:Constants.IconFontFamily,
    ),
    color: Color(AppColors.ConversationMuteIconColor),
    size: Constants.ConversationMuteIconSize,
    );


    return Container(
      height: 68.0,
      decoration: BoxDecoration(
          color: Color(AppColors.ConversationItemColor),
          border: BorderDirectional(
              bottom: BorderSide(
                  color: Color(AppColors.DividerColor),
                  width: Constants.DividerWidth))),
      padding: const EdgeInsets.all(10.0),
      // alignment: Alignment.center,
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[
          avatarContainer,
          Container(
            width: 10.0,
          ),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Text(
                  conversation.title,
                  style: AppStyles.TitleStyle,
                  overflow: TextOverflow.ellipsis,
                ),
                Container(
                  height: 6.0,
                ),
                Text(
                  conversation.des,
                  style: AppStyles.DescStyle,
                  overflow: TextOverflow.ellipsis,
                )
              ],
            ),
          ),
          Column(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              Text(
                conversation.updateAt,
                style: AppStyles.DescStyle,
              ),
              conversation.isMute?muteIcon:Container(),
            ],
          ),
        ],
      ),
    );
  }
}

//其他设备
class _DeviceInfoItem extends StatelessWidget{
  _DeviceInfoItem({this.device=Device.MAC}):assert(device!=null);
  final Device device;
  @override
  Widget build(BuildContext context){
    return Container(
      padding: const EdgeInsets.fromLTRB(24.0, 10.0, 24.0, 10.0),
      decoration: BoxDecoration(
        color: Color(AppColors.DeviceInfoItemBg),
        border: BorderDirectional(
          bottom: BorderSide(
            color: Color(AppColors.DividerColor),
            width: Constants.DividerWidth,
          )
        )
      ),
      child: Row(
         mainAxisAlignment: MainAxisAlignment.start,
         crossAxisAlignment: CrossAxisAlignment.center,
         children: <Widget>[
           Icon(IconData(
             0xe640,
             fontFamily: Constants.IconFontFamily,
           ),size: 24.0,),
           SizedBox(width: 16.0,),
           Text("微信已登陆，手机通知已关闭",style:AppStyles.DeviceInfoItemTextStyle),
         ],
      ),
    );
  }
}